package com.guomao.controller;


import com.guomao.entity.Brand;
import com.guomao.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//@Controller
@RestController
public class BrandController {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    BrandService brandService;

    @RequestMapping("/brand_getBrands")
//    @ResponseBody
    public List<Brand> getBrands() {
//        redisTemplate.opsForList();

        //设置redistemplate的序列化器 专门处理value是ArrayList集合的类型 以json格式存储 中文能够正常显示
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(ArrayList.class));
        List<Brand> list = (ArrayList<Brand>) redisTemplate.opsForValue().get("all_brand");
        if (list == null) {
            System.out.println("缓存中没有轮播图数据....查询数据库");
            list = brandService.list();
            redisTemplate.opsForValue().set("all_brand", list);
            //set all_brand
        } else {
            System.out.println("缓存中有轮播图数据,不用管查询数据库啦....");
        }
        return list;
    }

    @RequestMapping("/brand_delete")
    public boolean deleteBrand(@RequestParam Integer id) {
        return brandService.removeById(id);
    }
}
